# -*- coding:utf-8 -*-

from django import http, shortcuts
import urllib2, re


def index(request):

    from exscore.parser import Xscores
    
    parser = Xscores.Xscores('http://www.xscores.com/LiveScore.do?state=promptHockeyTable&sport=4&country=18712,17512,15063,10589,5537,4193,103,113&table=LEAGUE,18712,17512,15063,10589,5537,4193,103,113&league=18730,17530,15081,10590,5538,4194,104,114&season=18779,17569,15120,10592,5540,4196,106,116&year=14030&groupBy=1')
    
    table = parser.getChampTable()
    
    return shortcuts.render_to_response('grid.html', { 'champTable': table })
    return http.HttpResponse('');


def xscores(request):
    teamUrlFormat = r'http://www.xscores.com/LiveScore.do?state=prompt%sLeagueTeam&cid=%s&tsscid=%s'
    
    url    = 'http://www.xscores.com/LiveScore.do?state=promptHockeyTable&sport=4&country=18712,17512,15063,10589,5537,4193,103,113&table=LEAGUE,18712,17512,15063,10589,5537,4193,103,113&league=18730,17530,15081,10590,5538,4194,104,114&season=18779,17569,15120,10592,5540,4196,106,116&year=14030&groupBy=1'
    req    = urllib2.Request(url, None, { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' })
    result = urllib2.urlopen(req).read().decode('windows-1251')
    
    
    gameType = re.search('state=prompt(\w+)Table', url).group(1)
    
    tableUrls = []
    for (cid, tsscid) in re.findall(r'javascript\:showTeam\((\d+)\, (\d+)\)\;', result, re.I):
        tableUrls.append(teamUrlFormat % (gameType, cid, tsscid))
    
    
    req    = urllib2.Request(tableUrls[0], None, { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' })
    result = urllib2.urlopen(req).read().decode('windows-1251')
    
    
    resultTbl = re.findall(r'<table[^>]*>(.+?)</table>', result, re.S)[1]
    
    date = None
    out  = []
    for row in re.findall(r'<tr[^>]*>(.+?)</tr>', resultTbl, re.S):
        
        dateMatch = re.findall(r'<b>(\d+-\d+-\d+)</b>', row)
        if dateMatch:
            date = dateMatch[0]
        elif date:
            tds = re.findall(r'<td[^>]*>(.+?)</td>', row, re.S)
            out.append(date + ' >> ' + '\t\t'.join(tds))
        
        
        
        
        
        
    result = '\n--------------\n'.join(out)
    
    #return shortcuts.render_to_response('base.html')
    return http.HttpResponse('<pre>' + result);

    




def parimatch(request):

    url = 'http://www.xscores.com/LiveScore.do?state=promptHockeyLeagueTeam&sport=4&country=18712,17512,15063,10589,5537,4193,103,113&table=LEAGUE,18712,17512,15063,10589,5537,4193,103,113&league=18730,17530,15081,10590,5538,4194,104,114&season=18779,17569,15120,10592,5540,4196,106,116&year=14030&groupBy=1&cid=14030&tsscid=300128'

    req    = urllib2.Request('http://www.parimatch.com/stats.html?SK=0&Sec=12', None, { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' })
    result = urllib2.urlopen(req).read().decode('windows-1251')
    
    
    result = re.findall(r'<table[^>]+>(.+?)</table>', result, re.I)
    
    result = re.findall(r'<tr[^>]*>(.+?)</tr>', result[0], re.I)

    out = []
    for row in result:
        row = re.findall(r'<td[^>]*>(.+?)</td>', row, re.I)
        
        link = re.findall("WO\('([^']+?)'[^>]+>([^<]+)", row[1], re.I)
        if link:
            row[1] = ' / '.join(link[0])
        
        out.append('\t'.join(row))
        
    result = '\n\n'.join(out)
    
    #return shortcuts.render_to_response('base.html')
    return http.HttpResponse('<pre>' + result);
    